<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" @Launchform="Launchform5" :formList="formList1" @findClicked="findData" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '20px' }">
      <div style="width: 80%">
        <h1>{{ $t(`Print.InstructionUsageDetails`) }}</h1>
        <Find
          class="FindList"
          :searchImg="false"
          style="margin-left: 510px"
          :OriginalFilter="{ static: true }"
          @Launchform="Launchform6"
          :formList="formList2"
        />
        <Table :tableHeader="tableHeader" :tableData="InventoryAccountInquiryMethodsTableDate" :TableHeight="95" />
      </div>
      <PageModule :PageData="{ buttonS: true, total: totalList }" @pageChange="pageChange1" @pageSizeChange="pageSizeChange1" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'InventoryAccountInquiryKl'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { InventoryAccountInquiry, dataRefresh } from '@/api/AccountingDepartment/ProductionLabel/InventoryAccountInquiryKl'
import fileRequest from '@/utils/fileRequest'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
// 数据刷新
const dataRefreshMethods = async () => {
  try {
    const result = await dataRefresh()
    if (result.code === 200) {
      InventoryAccountInquiryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('刷新出错')
  }
}

// 主题查询

const formatCurrentDate = (date: any) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1 // 月份从0开始，所以要加1
  const day = date.getDate()
  let newMonth = month > 9 ? month : '0' + month // 修正为两位数
  let newDay = day > 9 ? day : '0' + day // 修正为两位数
  return `${year}-${newMonth}-${newDay}`
}

const currentDate = ref(new Date())

// 创建一个新的 Date 对象，设置为昨天的日期
const yesterdayDate = new Date(currentDate.value)
yesterdayDate.setDate(currentDate.value.getDate() - 1) // 将日期设置为昨天

// 现在可以安全地使用 formatCurrentDate 函数
const InventoryAccountInquiryParms = ref<any>({
  ddate: formatCurrentDate(yesterdayDate), // 使用昨天的日期
  lotNo: '',
  orderFile: '',
  orderType: '',
  pageNum: 1,
  pageSize: 40,
  positiveFile: [],
  processCode: '',
  style: ''
})

const Launchform6 = (row: any) => {
  if (row[0].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('当日入库')) {
      InventoryAccountInquiryParms.value.positiveFile.push('dayInQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('当日入库')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }

  if (row[1].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('累计入库')) {
      InventoryAccountInquiryParms.value.positiveFile.push('ttlInQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('累计入库')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }

  if (row[2].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('欠数')) {
      InventoryAccountInquiryParms.value.positiveFile.push('shortInQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('欠数')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }

  if (row[3].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('当日出库')) {
      InventoryAccountInquiryParms.value.positiveFile.push('dayOutQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('当日出库')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }

  if (row[4].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('累计出库')) {
      InventoryAccountInquiryParms.value.positiveFile.push('ttlOutQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('累计出库')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }

  if (row[5].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('未发数量')) {
      InventoryAccountInquiryParms.value.positiveFile.push('shortOutQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('未发数量')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }

  if (row[6].checked) {
    // 如果勾选，添加 '当日入库'
    if (!InventoryAccountInquiryParms.value.positiveFile.includes('库存')) {
      InventoryAccountInquiryParms.value.positiveFile.push('stockQty')
    }
  } else {
    // 如果取消勾选，移除 '当日入库'
    const index = InventoryAccountInquiryParms.value.positiveFile.indexOf('库存')
    if (index > -1) {
      InventoryAccountInquiryParms.value.positiveFile.splice(index, 1) // 从数组中移除
    }
  }
  InventoryAccountInquiryMethods()
}

const Launchform5 = (row: any) => {
  InventoryAccountInquiryParms.value.lotNo = row[0].inputValue
  InventoryAccountInquiryParms.value.style = row[1].inputValue
  InventoryAccountInquiryParms.value.ddate = row[3].dateValue
  InventoryAccountInquiryParms.value.processCode = row[2].selectValue
  InventoryAccountInquiryMethods()
}

const InventoryAccountInquiryMethodsTableDate = ref([])

const totalList = ref(0)

const pageChange1 = (val: any) => {
  InventoryAccountInquiryParms.value.pageNum = val
  InventoryAccountInquiryMethods()
}

const pageSizeChange1 = (val: any) => {
  InventoryAccountInquiryParms.value.pageSize = val
  InventoryAccountInquiryMethods()
}

const InventoryAccountInquiryMethods = async () => {
  try {
    const result = await InventoryAccountInquiry(InventoryAccountInquiryParms.value)
    InventoryAccountInquiryMethodsTableDate.value = result.list
    totalList.value = result.total
  } catch (error) {
    JMessage.error('查询出错')
  }
}

onMounted(() => {
  InventoryAccountInquiryMethods()
})

const formList1 = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '部门',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: '1',
        optionValue: '01',
        optionLabel: '冲裁'
      },
      {
        optionKey: '2',
        optionValue: '02',
        optionLabel: '高频'
      },
      {
        optionKey: '3',
        optionValue: '03',
        optionLabel: '配套中心'
      },
      {
        optionKey: '4',
        optionValue: '',
        optionLabel: ''
      }
    ]
  },
  {
    type: 'dateSingle',
    dateLabel: '日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '冲裁日报表'
  },
  {
    type: 'button',
    checkboxLabel: '外协日报表'
  },
  {
    type: 'button',
    checkboxLabel: '配套仓日报表'
  },
  {
    type: 'button',
    checkboxLabel: '外协欠数表'
  },
  {
    type: 'button',
    checkboxLabel: '数据刷新'
  }
])

const formList2 = ref([
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '',
    checked: false
  }
])

const findData = async (a: any) => {
  if (a === '数据刷新') {
    dataRefreshMethods()
  }
  InventoryAccountInquiryParms.value.pageNum = -1
  InventoryAccountInquiryParms.value.pageSize = -1
  if (a === '冲裁日报表') {
    // InventoryAccountInquiryParms.value.ddate =  formatCurrentDate(yesterdayDate)
    InventoryAccountInquiryParms.value.processCode = '01'
    if (InventoryAccountInquiryParms.value.ddate === '' || InventoryAccountInquiryParms.value.processCode === '') {
      JMessage.warning('导出时日期和部门不可为空')
    } else {
      const res = (await fileRequest.post(`/fq/StkAcc/export/process`, InventoryAccountInquiryParms.value)) as any
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = `冲裁日报表${formatCurrentDate(yesterdayDate)}.xlsx`
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }

  if (a === '外协日报表') {
    // InventoryAccountInquiryParms.value.ddate =  formatCurrentDate(yesterdayDate)
    InventoryAccountInquiryParms.value.processCode = '02'
    if (InventoryAccountInquiryParms.value.ddate === '' || InventoryAccountInquiryParms.value.processCode === '') {
      JMessage.warning('导出时日期和部门不可为空')
    } else {
      const res = (await fileRequest.post(`/fq/StkAcc/export/process`, InventoryAccountInquiryParms.value)) as any
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = `外协日报表${formatCurrentDate(yesterdayDate)}.xlsx`
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }

  if (a === '配套仓日报表') {
    // InventoryAccountInquiryParms.value.ddate =  formatCurrentDate(yesterdayDate)
    InventoryAccountInquiryParms.value.processCode = '03'
    if (InventoryAccountInquiryParms.value.ddate === '' || InventoryAccountInquiryParms.value.processCode === '') {
      JMessage.warning('导出时日期和部门不可为空')
    } else {
      const res = (await fileRequest.post(`/fq/StkAcc/export/process`, InventoryAccountInquiryParms.value)) as any
      const link = document.createElement('a') // 利用a标签自定义下载文件名
      let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
      link.style.display = 'none'
      //设置连接
      link.href = window.URL.createObjectURL(blob)
      const FileName = `配套仓日报表${formatCurrentDate(yesterdayDate)}.xlsx`
      link.download = FileName //自定义文件名字
      document.body.appendChild(link)
      //模拟点击事件
      link.click()
      document.body.removeChild(link)
    }
  }
  InventoryAccountInquiryParms.value.pageNum = 1
  InventoryAccountInquiryParms.value.pageSize = 40
}

// 表格数据

const tableHeader = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '轮次',
    prop: 'lsNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '订单量',
    prop: 'ordQty'
  },
  {
    label: '当日入库',
    prop: 'dayInQty'
  },
  {
    label: '累计入库',
    prop: 'ttlInQty'
  },
  {
    label: '欠数',
    prop: 'shortInQty'
  },
  {
    label: '当日出库',
    prop: 'dayOutQty'
  },
  {
    label: '累计出库',
    width: '55',
    prop: 'ttlOutQty'
  },

  {
    label: '未发数量',
    prop: 'shortOutQty'
  },

  {
    label: '库存',
    prop: 'stockQty'
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
:deep(.FindList .el-form-item__content) {
  margin-left: 70px !important;
}
</style>
